/*
 *  Class name: LoginHandler.java
 *  Version: 1.0
 *  Date: 7.6.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.restclient.utils;

import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.LoginPanel;
import java.util.function.Predicate;
import javax.swing.JOptionPane;

import static cz.muni.fi.pa165.soccergamesarchive.restclient.utils.JOptionDialogWindowHandler.showCustomYesNoDialogWindow;

/**
 *
 * @author Michal Ďuriš
 */
public class LoginHandler {

    private LoginHandler() {
    }

    public static LoginCredentials getLoginCredentials() {

        LoginPanel loginPanel = new LoginPanel();
        
        Predicate<String> emptyStringPredicate = string -> string == null || string.isEmpty();

        do {
            
            if(showCustomYesNoDialogWindow(null, "Login", loginPanel) != JOptionPane.OK_OPTION) {
                
                JOptionDialogWindowHandler.showInfoDialogWindow(null, "Info",
                        "Login credentials wasn't entered. "
                                + "Connection cannot be established. Sorry.");
                
                return null;
            }
            
        } while(emptyStringPredicate.test(loginPanel.getUsername()) || 
                emptyStringPredicate.test(loginPanel.getPassword()));
        
        return new LoginCredentials(loginPanel.getUsername(), loginPanel.getPassword());
    }
    
    public static class LoginCredentials {

        private final String username;
        private final String password;

        public LoginCredentials(String username, String password) {
            this.username = username;
            this.password = password;
        }

        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }
    }

}
